<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Image;
class Content extends Common
{
    public function index()
    {
        $modelId=input('model_id');
        $cateId=input('cate_id');
        if (!$modelId){
            $modelId=0;
        }
        $this->assign([
            'modelId'=>$modelId,
            'cateId'=>$cateId,
        ]);
        return view();
    }
    public function add()
    {
        //获取栏目
        $_cateRes=db('cate')->select();
        $cateRes=model('cate')->cateTree($_cateRes);
        $modelId=input('model_id');
        $cateId=input('cate_id');
        if (!$modelId){
            $modelId=0;
        }
        //获取当前模型自定义字段
        $diyFields=db('model_fields')->where(['model_id'=>$modelId])->select();
        $longTextFields=db('model_fields')->where(['model_id'=>$modelId,'field_type'=>9])->select();
        $this->assign([
            'cateRes'=>$cateRes,
            'modelId'=>$modelId,
            'cateId'=>$cateId,
            'diyFields'=>$diyFields,
            'longTextFields'=>$longTextFields,
        ]);
        return view();
    }
    public function edit()
    {
        return view();
    }
    //上传图片
    public function upimg(){
        $file=request()->file('img');
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/index');
        if ($info){
            $imgSrc=$info->getSaveName();
            $image=Image::open($imgSrc);
            $image->thumb(500,500,2)->save($imgSrc);
            echo $info->getSaveName();
        }else{
            echo $info->getError();
        }
    }
       //删除图片
    public function delImg(){
        $artid=input('artid');
        $imgurl= input('imgurl');
        $imgUrl=INDEX_IMG.$imgurl;
        $res=@unlink($imgUrl);
        if ($artid){
            db('archives')->where('id',$artid)->setField('litpic','');
        }
        if ($res){
            echo 1;//删除文件成功
        }else{
            echo 2;//删除文件失败
        }
    }
}
